<?php
	require("libraries/student.php");
	class Regions extends Student{
		public function __construct(){
			parent::__construct();
			$this->load->model("model_region");
			$this->load->helper("form");
			$this->load->library("form_validation");
			$this->load->library("string");
		}
		public function index(){
			$config['base_url'] = base_url().'admin/regions/index/';
			$config['total_rows'] = $this->model_region->count_all();
			$data['title'] = "Manage regions";
			$data['template'] = "regions/list_region";
			$config['per_page'] = 10;
			$config['uri_segment'] = 4;
			$config['next_link'] = "Next";
			$config['prev_link'] = "Prev";
			$this->load->library("pagination");
			$this->pagination->initialize($config); 
			$start = $this->uri->segment(4);
			$data['data'] = "";
			$data['act'] = 3;
			$data['listcate']= $this->model_region->listall($config['per_page'],$start);
			$this->load->view("layout",$data);
		}
		public function active(){
			$id   	= $this->uri->segment(4);
			$status	= $this->uri->segment(5);
			$db = array(
				"region_status" => $status 
			);
			$this->model_region->update_status($db,$id);
			redirect(base_url()."admin/regions");
		}
		public function add(){
		  	$data['title'] = "Add region";
			$data['template'] = "regions/add_region";
			$data['data'] = "";
			$data['act'] = 3;
			if($this->input->post("ok") != ""){
				$url = str_replace(' ', '-',strtolower($this->string->replace($this->input->post('region_title'))));
				$value = array(
						"region_title" => $this->input->post("region_title"),
                        "region_hide" => $this->input->post("region_hide"),
                        "region_des" => $this->input->post("region_des"),
						"region_rewrite" => $url,
						"region_order" => $this->input->post("region_order"),
						"region_show" => $this->input->post("region_show")
				);
				if($_FILES['region_img']['name'] != NULL){
					$config['upload_path']   = './uploads/regions';
					$config['allowed_types'] = 'jpg|png|jpeg|gif';
					$config['max_size']	= '1500';
					$config['max_width']  = '1000';
					$config['max_height']  = '1000';
					$config['encrypt_name'] = TRUE;
					$this->load->library('upload',$config);
					if(!$this->upload->do_upload("region_img")){
						$data = array('error' => $this->upload->display_errors());
						$data['title'] = "Add region";
						$data['template'] = "regions/add_region";
						$data['data'] = "";
						$data['act'] = 3;
						$data['destination'] = $this->model_region->listdestina();
						$this->load->view("layout",$data);
						return FALSE;
					}else{
						$data = $this->upload->data();
						$value['region_image'] = $data['file_name'];
						$this->createThumbnail($value['region_image'],"regions",160,160);
					}
				}
				$this->model_region->add($value);
				redirect(base_url()."admin/regions");
			}else{
				$this->load->view("layout",$data);
			}
		}
		public function update(){
			$id = $this->uri->segment(4);
			$data['title']= "Edit region";
			$data['template'] = "regions/edit_region";
			$data['data'] = "";
			$data['act'] = 3;
			$data['getdata'] = $this->model_region->getdata($id);
			if($data['getdata'] == NULL){ die("This region is not availabel");}
			$data['stt'] = $data['getdata']['destina_id'];
			if($this->input->post("ok") != ""){
				$url = str_replace(' ', '-',strtolower($this->string->replace($this->input->post('region_title'))));
				$value = array(
					"region_title" => $this->input->post("region_title"),
                    "region_hide" => $this->input->post("region_hide"),
                    "region_des" => $this->input->post("region_des"),
					"region_rewrite" => $url,
					"region_order" => $this->input->post("region_order"),
					"region_show" => $this->input->post("region_show")
				);
				if($_FILES['region_img']['name'] != NULL){
					$config['upload_path']   = './uploads/regions';
					$config['allowed_types'] = 'jpg|png|jpeg|gif';
					$config['max_size']	= '1500';
					$config['max_width']  = '1000';
					$config['max_height']  = '1000';
					$config['encrypt_name'] = TRUE;
					$this->load->library('upload',$config);
					if(!$this->upload->do_upload("region_img")){
						$data = array('error' => $this->upload->display_errors());
						$data['title']= "Edit region";
						$data['template'] = "regions/edit_region";
						$data['data'] = "";
						$data['act'] = 3;
						$data['getdata'] = $this->model_region->getdata($id);
						$data['destination'] = $this->model_region->listdestina();
						$data['stt'] = $data['getdata']['destina_id'];
						$this->load->view("layout",$data);
						return FALSE;
					}else{
						$data = $this->upload->data();
						$value['region_image'] = $data['file_name'];
						$this->createThumbnail($value['region_image'],"regions",160,160);
					}
				}
				$this->model_region->update($value,$id);
				redirect(base_url()."admin/regions/index");
			}else{
				$this->load->view("layout",$data);
			}
		}
		public function del(){
			$id = $this->uri->segment(4);
			$this->model_region->del($id);
			redirect(base_url()."admin/regions/index");
		}
		public function createThumbnail($fileName,$path,$width,$height){
			$this->load->library('image_lib');
			//$this->load->helper('thumbnail_helper');
			$config['image_library'] = 'gd2';
			$config['source_image'] = 'uploads/'.$path.'/'.$fileName;
			$config['new_image']    = 'uploads/'.$path.'/thumb/'.$fileName;
			$config['create_thumb'] = TRUE;
			$config['maintain_ratio'] = TRUE;
			$config['thumb_marker'] = FALSE;
			$config['width'] = $width;
			$config['height'] = $height;
			$this->image_lib->initialize($config); 
			$this->image_lib->resize();
			$this->image_lib->clear();
		}
	}